// pages/orderGoods/orderGoods.js
const { default: Toast } = require("../../dist/toast/toast.js");
const util = require("../../utils/util.js");
const dataUtil = require("../../utils/dataUtil.js");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    app:app,
    cloudUrl:app.globalData.cloudUrl,
    allMenus1:[
      {id:1,name:"购票订单",txtColor:"rgb(0,204,204)",select:true, border: "4rpx solid rgb(0, 204, 204)"},
      {id:2,name:"商城",txtColor:"#000000",select:false},
      {id:2,name:"酒店",txtColor:"#000000",select:false},
      {id:2,name:"餐饮",txtColor:"#000000",select:false},
      {id:2,name:"电子年卡订单",txtColor:"#000000",select:false},
    ],
    shopMenus:[
      {id:1,name:"全部",txtColor:"rgb(0,204,204)",select:false, border: "4rpx solid rgb(0, 204, 204)"},
      {id:2,name:"待付款",txtColor:"#000000",select:true},
      {id:3,name:"待发货",txtColor:"#000000",select:true},
      {id:4,name:"待收货",txtColor:"#000000",select:true},
      {id:5,name:"退款/售后",txtColor:"#000000",select:true}
    ],
    tickets:[{name:"楼上的",num:1,price:32,datestr:"2024-05-14"},{name:"楼上的",num:1,price:32,datestr:"2024-05-14"}],
    hotels:[{name:"标准大床房",num:1,price:32,datestr:"2024-05-14"},{name:"商务双人床",num:1,price:32,datestr:"2024-05-14"}],
    shops:[{key:"784984546545",status:"待付款",address:{name:"令狐冲",phone:"12345564466",address:"西湖区滨江路下沙公寓19号"},shopList:[{name:"烤肠",icon:"shopItem2.png"},{name:"腊肠",icon:"shopItem1.png"}]},{key:"784984546545",status:"待付款",shopList:[{name:"文具"}]}],
    tempShops:[],
    shopSelectIndex:0,
    dialogPop:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "订单",
    })
    console.log("=====order goods");
    console.log(options);
    if(options.type != null){//购票列表
      this.onAllMenuClick(null, options.type);
    }
  },
  updateInfo(){
    const app = getApp();
    const userInfo = app.globalData.userInfo;
    if(userInfo){
      var tks = userInfo.orderTickets;
      var hts = userInfo.orderHotels;
      var sps = userInfo.orderShops;
      for(var key in hts){
        hts[key].datestr = util.getDateStr(hts[key].time);
      }
      this.setData({tickets:tks,hotels:hts,shops:sps});//
      this.refreshShopList();
    }
  },
  onShopsClick(e){
    var info = e.currentTarget.dataset.item;
    info = JSON.stringify(info);
    //console.log("onShopsClick",info);
    util.gotoOrderShopsPage(info);
  },
  onTicketsClick(e){
    if(e.target.id == "ticketPay"){
      Toast("暂未开通微信支付");
      return;
    }
    var info = e.currentTarget.dataset.item;
    if(e.target.id == "ticketCancel"){
      dataUtil.deleteTicketOrder(info, this.onCallBack);
      return;
    }

    info = JSON.stringify(info);
    util.gotoOrderTicketsPage(info);
  },
  onHotelsClick(e){
    if(e.target.id == "pay"){
      Toast("暂未开通微信支付");
      return;
    }
    var info = e.currentTarget.dataset.item;
    if(e.target.id == "cancel"){
      dataUtil.deleteHotelOrder(info, this.onCallBack);
      return;
    }
    info = JSON.stringify(info);
    util.gotoOrderHotelsPage(info);
  },
  onCallBack(result){
    this.updateInfo();
  },
  onAllMenuClick(e, index=-1){
    var idx = 0;//
    if(index >= 0){
      idx = index;
    }
    else{
      idx = e.currentTarget.dataset.index;
    }
    this.data.allCurMenu1Index = idx;
    const menus = this.data.allMenus1;
    var menu = null;
    for(var i=0; i<menus.length; i++){
      menu = menus[i];
      if(idx == i){
        menu.select = true;
        menu.txtColor = "rgb(0,204,204)";
        menu.border = "2px solid rgb(0,204,204)";
      }
      else{
        menu.select = false;
        menu.txtColor = "rgb(0,0,0)";
        menu.border = "";
      }
    }
    this.setData({allMenus1:this.data.allMenus1});
  },
  onAllMenu1Click(e, index=-1){
    var idx = 0;
    if(index >= 0){
      idx = index;
    }
    else{
      idx = e.currentTarget.dataset.index;
    }
    this.data.shopSelectIndex = idx;
    const menus = this.data.shopMenus;
    var menu = null;
    for(var i=0; i<menus.length; i++){
      menu = menus[i];
      if(idx == i){
        menu.select = true;
        menu.txtColor = "rgb(0,204,204)";
        menu.border = "2px solid rgb(0,204,204)";
      }
      else{
        menu.select = false;
        menu.txtColor = "rgb(0,0,0)";
        menu.border = "";
      }
    }
    this.setData({shopMenus:this.data.shopMenus});
    this.refreshShopList();
  },
  refreshShopList(){
    const menu = this.data.shopMenus[this.data.shopSelectIndex];
    const arr = [];
    const shops = this.data.shops;
    for(var i=0; i<shops.length; i++){
      if(menu.name == "全部" || menu.name == shops[i].status){
        arr.push(shops[i]);
      }
    }
    this.setData({tempShops:arr});
  },
  /************商城*********** */
  onDailogClick(e){
    this.setData({dialogPop:true});
  },
  onShopPayClick(e){
    Toast("暂未开通微信支付");
  },
  onShopDeleteClick(e){
    console.log("删除商品，", e)
    var info = e.currentTarget.dataset.item;
    dataUtil.deleteShopOrder(info, this.onShopBack);
  },
  onShopBack(e){
    util.getAndSetUserInfo(this.onUserBack);
  },
  onUserBack(result){
    this.updateInfo();
  },
  onConfigClick(e){
    this.setData({dialogPop:false});
  },
  onCancelClick(e){
    this.setData({dialogPop:false});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    util.getAndSetUserInfo(this.onUserBack);
    //this.updateInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})